<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/javascript/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/javascript/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/javascript/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/javascript/images/logo.svg" color="#222">

<link rel="stylesheet" href="/javascript/css/main.css">


<link rel="stylesheet" href="/javascript/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/javascript/","scheme":"Muse","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="常见手写js面试题1 数组扁平化1234567891011121314151617181920212223242526272829303132333435363738394041424344&#x2F;&#x2F; const arr &#x3D; [1, [11, 11], [22, [222, 333, [4444, 5555, [66666, 77777]]]]];&#x2F;&#x2F;1 递归function flat(arr) &amp;#1">
<meta property="og:type" content="article">
<meta property="og:title" content="常见手写js面试题">
<meta property="og:url" content="http://yoursite.com/2020/10/10/%E5%B8%B8%E8%A7%81%E6%89%8B%E5%86%99js%E9%9D%A2%E8%AF%95%E9%A2%98/index.html">
<meta property="og:site_name" content="javascript">
<meta property="og:description" content="常见手写js面试题1 数组扁平化1234567891011121314151617181920212223242526272829303132333435363738394041424344&#x2F;&#x2F; const arr &#x3D; [1, [11, 11], [22, [222, 333, [4444, 5555, [66666, 77777]]]]];&#x2F;&#x2F;1 递归function flat(arr) &amp;#1">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-10-10T14:35:41.329Z">
<meta property="article:modified_time" content="2020-10-13T16:46:40.008Z">
<meta property="article:author" content="HC">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/10/10/%E5%B8%B8%E8%A7%81%E6%89%8B%E5%86%99js%E9%9D%A2%E8%AF%95%E9%A2%98/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>常见手写js面试题 | javascript</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/javascript/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">javascript</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/javascript/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/javascript/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="en">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/10/10/%E5%B8%B8%E8%A7%81%E6%89%8B%E5%86%99js%E9%9D%A2%E8%AF%95%E9%A2%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/javascript/images/avatar.gif">
      <meta itemprop="name" content="HC">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="javascript">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          常见手写js面试题
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-10-10 22:35:41" itemprop="dateCreated datePublished" datetime="2020-10-10T22:35:41+08:00">2020-10-10</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2020-10-14 00:46:40" itemprop="dateModified" datetime="2020-10-14T00:46:40+08:00">2020-10-14</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="常见手写js面试题"><a href="#常见手写js面试题" class="headerlink" title="常见手写js面试题"></a>常见手写js面试题</h1><h2 id="1-数组扁平化"><a href="#1-数组扁平化" class="headerlink" title="1 数组扁平化"></a>1 数组扁平化</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// const arr = [1, [11, 11], [22, [222, 333, [4444, 5555, [66666, 77777]]]]];</span></span><br><span class="line"><span class="comment">//1 递归</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">flat</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> newArr = [];</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">Array</span>.isArray(arr[i])) &#123;</span><br><span class="line">            newArr = newArr.concat(flat(arr[i]));</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            newArr.push(arr[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> newArr;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//2 原型上提供的方法</span></span><br><span class="line">arr.flat([depth]);<span class="comment">// depth遍历的层数，默认是1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 简单实现，没有实现depth</span></span><br><span class="line"><span class="built_in">Array</span>.prototype.flat = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> newArr = [];</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>.length; i ++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">Array</span>.isArray(<span class="keyword">this</span>[i])) &#123;</span><br><span class="line">            <span class="comment">// 递归，注意的是，concat不改变原数组,因此要用newArr去接收的到的数组</span></span><br><span class="line">            newArr = newArr.concat(<span class="keyword">this</span>[i].flat());</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            newArr.push(<span class="keyword">this</span>[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> newArr;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//3 reduce</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">flat2</span> (<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> arr.reduce(<span class="function">(<span class="params">memo, current</span>) =&gt;</span> &#123; </span><br><span class="line">        <span class="keyword">return</span> memo.concat(<span class="built_in">Array</span>.isArray(current) ? flat2(current) : current); </span><br><span class="line">    &#125;, [])</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//4 正则替换</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">flat3</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> arrStr = <span class="built_in">JSON</span>.stringify(arr);</span><br><span class="line">    <span class="keyword">const</span> res = arrStr.replace(<span class="regexp">/\[|\]/g</span>, <span class="string">''</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">JSON</span>.parse(<span class="string">`[<span class="subst">$&#123;res&#125;</span>]`</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-数组去重"><a href="#2-数组去重" class="headerlink" title="2 数组去重"></a>2 数组去重</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">5</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 1 使用对象的不可重特性</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique1</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> obj = &#123;&#125;;</span><br><span class="line">    <span class="keyword">const</span> newArr = [];</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length; i ++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (!obj[arr[i]]) &#123;</span><br><span class="line">            obj[arr[i]] = <span class="string">'a'</span>;</span><br><span class="line">            newArr.push(arr[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> newArr;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 2 利用数组的filter</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique2</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> arr.filter(<span class="function">(<span class="params">item, index, arr</span>) =&gt;</span> arr.indexOf(item) === index);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 3 使用新的数据结构Map</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">unique3</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Array</span>.from(<span class="keyword">new</span> <span class="built_in">Set</span>(arr));</span><br><span class="line">    <span class="comment">// 也可以使用下面的转换</span></span><br><span class="line">    <span class="comment">// return [...new Set(arr)];</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="3-类数组转为真数组"><a href="#3-类数组转为真数组" class="headerlink" title="3 类数组转为真数组"></a>3 类数组转为真数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">transfer</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// arguments || 获取使用document.querySelectorAll("xxx"), document.getElementsByTagName("*")...等都是典型的类数组</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1 Array.from(argument);</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">Array</span>.from(<span class="built_in">arguments</span>));</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2 Array.prototype.slice.call(arguments);</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">Array</span>.prototype.slice.call(<span class="built_in">arguments</span>));</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3 Array.prototype.concat.apply([], arguments);</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">Array</span>.prototype.concat.apply([], <span class="built_in">arguments</span>));</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4 扩展运算符</span></span><br><span class="line">    <span class="built_in">console</span>.log([...arguments]);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">transfer(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>);</span><br></pre></td></tr></table></figure>

<h2 id="4-Array-prototype-filter"><a href="#4-Array-prototype-filter" class="headerlink" title="4 Array.prototype.filter()"></a>4 Array.prototype.filter()</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Array</span>.prototype.filter = <span class="function"><span class="keyword">function</span> (<span class="params">callback</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> arr = [];</span><br><span class="line">    <span class="keyword">const</span> len = <span class="keyword">this</span>.length;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (callback(<span class="keyword">this</span>[i], i, <span class="keyword">this</span>)) &#123;</span><br><span class="line">            arr.push(<span class="keyword">this</span>[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="5-Array-prototype-map"><a href="#5-Array-prototype-map" class="headerlink" title="5. Array.prototype.map()"></a>5. Array.prototype.map()</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Array</span>.prototype.map = <span class="function"><span class="keyword">function</span> (<span class="params">callback</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> arr = [];</span><br><span class="line">    <span class="keyword">const</span> len = <span class="keyword">this</span>.length;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        arr.push(callback(<span class="keyword">this</span>[i], i, arr));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> arr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="6-Array-prototype-forEach"><a href="#6-Array-prototype-forEach" class="headerlink" title="6. Array.prototype.forEach()"></a>6. Array.prototype.forEach()</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Array</span>.prototype.forEach = <span class="function"><span class="keyword">function</span> (<span class="params">callback</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> len = <span class="keyword">this</span>.length;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        callback(<span class="keyword">this</span>[i], i, <span class="keyword">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'my'</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="7-Array-prototype-reduce"><a href="#7-Array-prototype-reduce" class="headerlink" title="7. Array.prototype.reduce()"></a>7. Array.prototype.reduce()</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Array</span>.prototype.reduce = <span class="function"><span class="keyword">function</span> (<span class="params">callback, initVal</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">this</span>.length === <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> initVal;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> len = <span class="keyword">this</span>.length;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (initVal) &#123;</span><br><span class="line">            initVal = callback(initVal, <span class="keyword">this</span>[i], i, <span class="keyword">this</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            initVal = callback(<span class="keyword">this</span>[i], <span class="keyword">this</span>[i + <span class="number">1</span>], i + <span class="number">1</span>, <span class="keyword">this</span>);</span><br><span class="line">            i++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> initVal;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="8-Function-prototype-apply"><a href="#8-Function-prototype-apply" class="headerlink" title="8. Function.prototype.apply()"></a>8. Function.prototype.apply()</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Array</span>.prototype.reduce = <span class="function"><span class="keyword">function</span> (<span class="params">callback, initVal</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">this</span>.length === <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> initVal;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> len = <span class="keyword">this</span>.length;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (initVal) &#123;</span><br><span class="line">            initVal = callback(initVal, <span class="keyword">this</span>[i], i, <span class="keyword">this</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            initVal = callback(<span class="keyword">this</span>[i], <span class="keyword">this</span>[i + <span class="number">1</span>], i + <span class="number">1</span>, <span class="keyword">this</span>);</span><br><span class="line">            i++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> initVal;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="9-Function-prototype-bind"><a href="#9-Function-prototype-bind" class="headerlink" title="9. Function.prototype.bind()"></a>9. Function.prototype.bind()</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Function</span>.prototype.apply = <span class="function"><span class="keyword">function</span> (<span class="params">ctx, args</span>) </span>&#123;</span><br><span class="line">    ctx.fn = <span class="keyword">this</span>;</span><br><span class="line">    <span class="keyword">const</span> result = ctx.fn(...args);</span><br><span class="line">    <span class="keyword">delete</span> ctx.fn;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="10-Function-prototype-call"><a href="#10-Function-prototype-call" class="headerlink" title="10. Function.prototype.call()"></a>10. Function.prototype.call()</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Function</span>.prototype.call = <span class="function"><span class="keyword">function</span> (<span class="params">ctx, ...args</span>) </span>&#123;</span><br><span class="line">    ctx.fn = <span class="keyword">this</span>;</span><br><span class="line">    <span class="keyword">const</span> result = ctx.fn(...args);</span><br><span class="line">    <span class="keyword">delete</span> ctx.fn;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="11-Function-prototype-bind"><a href="#11-Function-prototype-bind" class="headerlink" title="11. Function.prototype.bind()"></a>11. Function.prototype.bind()</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Array</span>.prototype.bind = <span class="function"><span class="keyword">function</span> (<span class="params">ctx, ...args</span>) </span>&#123;</span><br><span class="line">    ctx.fn = <span class="keyword">this</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params">...args2</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">const</span> totalArgs = args.concat(args2);</span><br><span class="line">        <span class="keyword">return</span> ctx.fn(...totalArgs);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="12-函数防抖-debounce"><a href="#12-函数防抖-debounce" class="headerlink" title="12. 函数防抖(debounce)"></a>12. 函数防抖(debounce)</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 多次调用只会执行一次</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">debounce</span>(<span class="params">fn, time</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> timer = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params">...args</span>) </span>&#123;</span><br><span class="line">        <span class="comment">// 不断的触发这个函数最终等到停下触发的时候才会在定时器完成去执行对应的函数</span></span><br><span class="line">        <span class="comment">// 每次调用这个函数看如果已经开过定时器，清楚重新开启</span></span><br><span class="line">        <span class="keyword">if</span> (timer) &#123;</span><br><span class="line">            clearTimeout(timer);</span><br><span class="line">        &#125;</span><br><span class="line">        timer = setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">            fn(...args);</span><br><span class="line">        &#125;, time)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="13-函数节流"><a href="#13-函数节流" class="headerlink" title="13. 函数节流"></a>13. 函数节流</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 多次调用只会在间隔时间调用一次</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">throttle</span>(<span class="params">fn, time</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> timer = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params">...args</span>) </span>&#123;</span><br><span class="line">        <span class="comment">// 如果已经开了定时器，什么都不做</span></span><br><span class="line">        <span class="keyword">if</span> (timer) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        timer = setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">            fn(...args);</span><br><span class="line">            <span class="comment">// 定时器到了记得将timer变为null</span></span><br><span class="line">            timer = <span class="literal">null</span>;</span><br><span class="line">        &#125;, time)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">throttle2</span>(<span class="params">fn, time</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> timer = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params">...args</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!timer || (<span class="built_in">Date</span>.now() - timer) &gt; time) &#123;</span><br><span class="line">            timer = <span class="built_in">Date</span>.now();</span><br><span class="line">            setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                fn(...args);</span><br><span class="line">            &#125;, time)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="14-柯里化"><a href="#14-柯里化" class="headerlink" title="14. 柯里化"></a>14. 柯里化</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 柯里化就是用于固定参数的</span></span><br><span class="line"><span class="keyword">const</span> curry = <span class="function">(<span class="params">fn, ...args</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 第一次调用参数就满足</span></span><br><span class="line">    <span class="keyword">if</span> (args.length &gt;= fn.length) &#123;</span><br><span class="line">        <span class="keyword">return</span> fn(...args);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params">...newArgs</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">const</span> totalArgs = args.concat(newArgs);</span><br><span class="line">            <span class="comment">// 参数已经够了</span></span><br><span class="line">            <span class="keyword">if</span> (totalArgs.length &gt;= fn.length) &#123;</span><br><span class="line">                <span class="keyword">return</span> fn(...totalArgs);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">// 参数还不满足，递归</span></span><br><span class="line">                totalArgs.unshift(fn);</span><br><span class="line">                <span class="keyword">return</span> curry(...totalArgs);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="15-instanceof"><a href="#15-instanceof" class="headerlink" title="15. instanceof"></a>15. instanceof</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> oInstanceof =  <span class="function">(<span class="params">origin, target</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> tempPrototype = <span class="built_in">Object</span>.getPrototypeOf(origin);</span><br><span class="line">    <span class="keyword">while</span>(tempPrototype) &#123;</span><br><span class="line">        <span class="keyword">if</span> (tempPrototype === target.prototype) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125; </span><br><span class="line">        tempPrototype = <span class="built_in">Object</span>.getPrototypeOf(tempPrototype);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="16-原型继承"><a href="#16-原型继承" class="headerlink" title="16 原型继承"></a>16 原型继承</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Child</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 访问父亲的属性</span></span><br><span class="line">    Parent.call(<span class="keyword">this</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Parent</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 访问父亲原型上的方法</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 1 使用Object.create</span></span><br><span class="line"><span class="keyword">const</span> extend = <span class="function">(<span class="params">Child, Parent</span>) =&gt;</span> &#123;</span><br><span class="line">    Child.prototype = <span class="built_in">Object</span>.create(Parent.prototype);</span><br><span class="line">    <span class="comment">// Child的原型改变了 那么constructor也会改变，所以把constructor指向Child</span></span><br><span class="line">    Child.prototype.constructor = Child;</span><br><span class="line">    <span class="comment">// 可以在孩子的原型的uber直接访问到Parent.prototype</span></span><br><span class="line">    Child.prototype.uber = Parent.prototype;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// extend(Child, Parent);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 2 使用中间函数</span></span><br><span class="line"><span class="keyword">const</span> extend2 = <span class="function">(<span class="params">Child, Parent</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> Fn = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;;</span><br><span class="line">    Fn.prototype = Parent.prototype;</span><br><span class="line">    Child.prototype = <span class="keyword">new</span> Fn();</span><br><span class="line">    Child.prototype.constructor = Child;</span><br><span class="line">    Child.prototype.uber = Parent.prototype;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 3 雅虎版 在方法2的基础上使用立即执行函数</span></span><br><span class="line"><span class="keyword">const</span> extend3 = <span class="function">(<span class="params">(</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> Fn = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;;</span><br><span class="line">    <span class="keyword">return</span> <span class="function">(<span class="params">Child, Parent</span>) =&gt;</span> &#123;</span><br><span class="line">        Fn.prototype = Parent.prototype;</span><br><span class="line">        Child.prototype = <span class="keyword">new</span> Fn();</span><br><span class="line">        Child.prototype.constructor = Child;</span><br><span class="line">        Child.prototype.uber = Parent.prototype;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line"><span class="comment">// 4 语言本身提供的方法</span></span><br><span class="line"><span class="keyword">const</span> extend4 = <span class="function">(<span class="params">Child, Parent</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">Object</span>.setPrototypeOf(Child.prototype, Parent.prototype);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 5 使用__proto__</span></span><br><span class="line"><span class="keyword">const</span> extend5 = <span class="function">(<span class="params">Child, Parent</span>) =&gt;</span> &#123;</span><br><span class="line">    Child.prototype.__proto__ =  Parent.prototype;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="17-Object-is"><a href="#17-Object-is" class="headerlink" title="17. Object.is()"></a>17. Object.is()</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 主要解决 -0 等于 +0  NaN不等于NaN的情况</span></span><br><span class="line"><span class="built_in">Object</span>.is = <span class="function">(<span class="params">value1, value2</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span>(value1 === <span class="number">0</span> &amp;&amp; value2 === <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span>  (<span class="number">1</span> / value1) === (<span class="number">1</span> / value2);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> nan = <span class="string">"NaN"</span>;</span><br><span class="line">    <span class="keyword">if</span> (nan === value1.toString() &amp;&amp; nan === value2.toString() ) &#123;</span><br><span class="line">        <span class="keyword">return</span> value1 !== value2;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> value1 === value2;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="18-Object-create"><a href="#18-Object-create" class="headerlink" title="18. Object.create()"></a>18. Object.create()</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Object</span>.create = <span class="function">(<span class="params">proto</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> Fn = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;;</span><br><span class="line">    Fn.prototype = proto;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> Fn();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="19-Object-assign"><a href="#19-Object-assign" class="headerlink" title="19. Object.assign()"></a>19. Object.assign()</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 浅拷贝</span></span><br><span class="line"><span class="built_in">Object</span>.assign = <span class="function">(<span class="params">target, ...args</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> len = args.length;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; len; i ++) &#123;</span><br><span class="line">        <span class="keyword">const</span> obj = args[i];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> prop <span class="keyword">in</span> obj) &#123;</span><br><span class="line">            target[prop] = obj[prop];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> target;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="20-深拷贝"><a href="#20-深拷贝" class="headerlink" title="20. 深拷贝"></a>20. 深拷贝</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">deepClone</span>(<span class="params">origin, oMap = new WeakMap(</span>)) </span>&#123;</span><br><span class="line">    <span class="comment">// 原始类型 number string boolean undefined symbol bigInt</span></span><br><span class="line">    <span class="keyword">if</span> (origin !== <span class="literal">null</span> &amp;&amp; <span class="keyword">typeof</span> origin !== <span class="string">'object'</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> origin;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (origin <span class="keyword">instanceof</span> <span class="built_in">Date</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (origin <span class="keyword">instanceof</span> <span class="built_in">RegExp</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">RegExp</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 防止相互引用</span></span><br><span class="line">    <span class="keyword">if</span> (oMap.has(origin)) &#123;</span><br><span class="line">        <span class="keyword">return</span> oMap.get(origin);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// new (&#123;&#125;.constructor) =&gt; &#123;&#125;, new ([].constructor) =&gt; []; </span></span><br><span class="line">    <span class="keyword">const</span> target = <span class="keyword">new</span> (origin.constructor);</span><br><span class="line">    oMap.set(origin, target);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> prop <span class="keyword">in</span> origin) &#123;</span><br><span class="line">        <span class="comment">// 只拷贝自身存在的对象，不管原型链上的对象</span></span><br><span class="line">        <span class="keyword">if</span> (origin.hasOwnProperty(prop)) &#123;</span><br><span class="line">            target[prop] = deepClone(origin[prop], oMap);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> target;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="21-当前网页有多少中html标签"><a href="#21-当前网页有多少中html标签" class="headerlink" title="21. 当前网页有多少中html标签"></a>21. 当前网页有多少中html标签</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">new Set([...document.getElementsByTagName(&quot;*&quot;)].map(item &#x3D;&gt; item.tagName)).size;</span><br></pre></td></tr></table></figure>

<h2 id="22-Promise"><a href="#22-Promise" class="headerlink" title="22. Promise"></a>22. Promise</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> PENDING = <span class="string">'PENDING'</span>;</span><br><span class="line"><span class="keyword">const</span> RESOLVED = <span class="string">'RESOLVED'</span>;</span><br><span class="line"><span class="keyword">const</span> REJECTED = <span class="string">'REJECTED'</span>;</span><br><span class="line"><span class="keyword">const</span> resolvePromise = <span class="function">(<span class="params">x, promise2, resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (promise2 === x) &#123; <span class="comment">// 如果x得到的是return出去的promise2 reject一个错误</span></span><br><span class="line">        <span class="keyword">return</span> reject(<span class="keyword">new</span> <span class="built_in">TypeError</span>(<span class="string">'err~~~~~~'</span>));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ((x !== <span class="literal">null</span> &amp;&amp; <span class="keyword">typeof</span> x === <span class="string">'object'</span>) || <span class="keyword">typeof</span> x === <span class="string">'function'</span>) &#123;</span><br><span class="line">        <span class="comment">// 防止多次调用</span></span><br><span class="line">        <span class="keyword">let</span> lock;</span><br><span class="line">        <span class="keyword">try</span> &#123; <span class="comment">// try 防止取值then出错</span></span><br><span class="line">            <span class="keyword">const</span> then = x.then;</span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">typeof</span> then === <span class="string">'function'</span>) &#123;</span><br><span class="line">                then.call(x, y =&gt; &#123;</span><br><span class="line">                    <span class="keyword">if</span> (lock) &#123;</span><br><span class="line">                        <span class="keyword">return</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                    lock = <span class="literal">true</span>;</span><br><span class="line">                    <span class="comment">// 处理 then(() =&gt; new Promise((resolve, reject) =&gt; resolve(new Promise((resolve, reject))))); then又resolve一个promise</span></span><br><span class="line">                    resolvePromise(y, promise2, resolve, reject);</span><br><span class="line">                &#125;, err =&gt; &#123;</span><br><span class="line">                    <span class="keyword">if</span> (lock) &#123;</span><br><span class="line">                        <span class="keyword">return</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                    lock = <span class="literal">true</span>;</span><br><span class="line">                    reject(err);</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">// then不是函数</span></span><br><span class="line">                resolve(x);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">            <span class="keyword">if</span> (lock) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            lock = <span class="literal">true</span>;</span><br><span class="line">            reject(e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 原始值 number string boolean undefined null symbol</span></span><br><span class="line">        resolve(x);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Promise</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(executor) &#123;</span><br><span class="line">        <span class="keyword">this</span>.status = PENDING;</span><br><span class="line">        <span class="comment">// 存resolved成功的结果</span></span><br><span class="line">        <span class="keyword">this</span>.value = <span class="literal">undefined</span>;</span><br><span class="line">        <span class="comment">// 存rejected失败的结果</span></span><br><span class="line">        <span class="keyword">this</span>.reason = <span class="literal">undefined</span>;</span><br><span class="line">        <span class="keyword">this</span>.onResolvedCallbacks = [];</span><br><span class="line">        <span class="keyword">this</span>.onRejectedCallbacks = [];</span><br><span class="line">        <span class="keyword">const</span> resolve = <span class="function">(<span class="params">value</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="comment">// resolve(new Promise((resolve, reject) =&gt; &#123;&#125;)) 处理这种情况</span></span><br><span class="line">            <span class="keyword">if</span> (value <span class="keyword">instanceof</span> <span class="built_in">Promise</span>) &#123;</span><br><span class="line">                <span class="comment">// 保证当前实例的resolve reject触发，得到值进行下一次的then的成功或失败</span></span><br><span class="line">                <span class="comment">// 一定要记得return,否则不会等待</span></span><br><span class="line">                <span class="comment">// Promise.resolve(new Promise((resolve, reject) =&gt; &#123;setTimeout(() =&gt; &#123;resolve('aaa'), 2000&#125;)&#125;));</span></span><br><span class="line">                <span class="keyword">return</span> value.then(resolve, reject);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">this</span>.status === PENDING) &#123;</span><br><span class="line">                <span class="comment">// 只有在pending才能触发成功</span></span><br><span class="line">                <span class="keyword">this</span>.status = RESOLVED;</span><br><span class="line">                <span class="keyword">this</span>.value = value;</span><br><span class="line">                <span class="comment">// 异步延迟触发resolve放到该回调中</span></span><br><span class="line">                <span class="keyword">this</span>.onResolvedCallbacks.forEach(<span class="function"><span class="params">fn</span> =&gt;</span> fn());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">const</span> reject = <span class="function">(<span class="params">reason</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">this</span>.status === PENDING) &#123;</span><br><span class="line">                <span class="keyword">this</span>.status = REJECTED;</span><br><span class="line">                <span class="keyword">this</span>.reason = reason;</span><br><span class="line">                <span class="comment">// 异步延迟触发reject放到该回调中</span></span><br><span class="line">                <span class="keyword">this</span>.onRejectedCallbacks.forEach(<span class="function"><span class="params">fn</span> =&gt;</span> fn());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// try目的是捕获new Promise内可能产生的错误</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            executor(resolve, reject);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">            <span class="comment">// 如果出错，直接reject</span></span><br><span class="line">            reject(e);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    then(onResolve, onReject) &#123;</span><br><span class="line">        <span class="comment">// then具有穿透效果</span></span><br><span class="line">        <span class="comment">// new Promise((resolve, reject) =&gt; resolve('11')).then().then().then((res) =&gt; &#123;console.log(res)&#125;);</span></span><br><span class="line">        <span class="comment">// new Promise((resolve, reject) =&gt; reject('err)).then().then().then(() =&gt; &#123;&#125;, (err) =&gt; &#123;console.log(err)&#125;);</span></span><br><span class="line">        onResolve = <span class="keyword">typeof</span> onResolve === <span class="string">"function"</span> ? onResolve : <span class="function"><span class="params">v</span> =&gt;</span> v;</span><br><span class="line">        <span class="comment">// 必须是throw err 如果是return err 那么会成为下一次的成功</span></span><br><span class="line">        onReject = <span class="keyword">typeof</span> onReject === <span class="string">"function"</span> ? onReject : <span class="function"><span class="params">err</span> =&gt;</span> &#123; <span class="keyword">throw</span> err &#125;;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">const</span> promise2 = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">this</span>.status === RESOLVED) &#123;</span><br><span class="line">                <span class="comment">// 1 这里模拟不了微任务，微任务是语言本身提供的，我这里只能用setTimeout代替</span></span><br><span class="line">                <span class="comment">// 2 能拿到promise2</span></span><br><span class="line">                setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                    <span class="comment">// 使用try防止执行onResolve内部报错</span></span><br><span class="line">                    <span class="keyword">try</span> &#123;</span><br><span class="line">                        <span class="keyword">const</span> x = onResolve(<span class="keyword">this</span>.value);</span><br><span class="line">                        resolvePromise(x, promise2, resolve, reject);</span><br><span class="line">                    &#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">                        reject(e);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;, <span class="number">0</span>)</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">this</span>.status === REJECTED) &#123;</span><br><span class="line">                setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                    <span class="keyword">try</span> &#123;</span><br><span class="line">                        <span class="keyword">const</span> x = onReject(<span class="keyword">this</span>.reason);</span><br><span class="line">                        resolvePromise(x, promise2, resolve, reject);</span><br><span class="line">                    &#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">                        reject(e);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;, <span class="number">0</span>)</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">this</span>.status === PENDING) &#123; <span class="comment">// 延迟加载的情况</span></span><br><span class="line">                <span class="keyword">this</span>.onResolvedCallbacks.push(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                    setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                        <span class="keyword">try</span> &#123;</span><br><span class="line">                            <span class="keyword">const</span> x = onResolve(<span class="keyword">this</span>.value);</span><br><span class="line">                            resolvePromise(x, promise2, resolve, reject);</span><br><span class="line">                        &#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">                            reject(e);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;, <span class="number">0</span>)</span><br><span class="line">                &#125;)</span><br><span class="line">                <span class="keyword">this</span>.onRejectedCallbacks.push(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                    setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                        <span class="keyword">try</span> &#123;</span><br><span class="line">                            <span class="keyword">const</span> x = onReject(<span class="keyword">this</span>.reason);</span><br><span class="line">                            resolvePromise(x, promise2, resolve, reject);</span><br><span class="line">                        &#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">                            reject(e);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;, <span class="number">0</span>)</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">return</span> promise2;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">static</span> resolve(value) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve</span>) =&gt;</span> &#123;</span><br><span class="line">            resolve(value);</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">static</span> reject(reason) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">            reject(reason);</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">static</span> race(arr) &#123;</span><br><span class="line">        <span class="keyword">const</span> len = arr.length;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">                <span class="keyword">const</span> p = arr[i];</span><br><span class="line">                <span class="keyword">if</span> (p <span class="keyword">instanceof</span> <span class="built_in">Promise</span>) &#123;</span><br><span class="line">                    p.then(resolve, reject);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    resolve(p);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">static</span> all(arr) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'my'</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">const</span> len = arr.length;</span><br><span class="line">            <span class="keyword">const</span> result = [];</span><br><span class="line">            <span class="keyword">let</span> i = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">const</span> processData = <span class="function">(<span class="params">index, value</span>) =&gt;</span> &#123;</span><br><span class="line">                result[index] = value;</span><br><span class="line">                <span class="keyword">if</span> (++i === len) &#123;</span><br><span class="line">                    resolve(result)</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">                <span class="keyword">const</span> p = arr[i];</span><br><span class="line">                <span class="keyword">if</span> (p <span class="keyword">instanceof</span> <span class="built_in">Promise</span>) &#123;</span><br><span class="line">                    p.then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">                        processData(i, res);</span><br><span class="line">                    &#125;, reject)</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    processData(i, p);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">catch</span>(errCallback) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.then(<span class="literal">null</span>, errCallback);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">finally</span>(callback) &#123;</span><br><span class="line">        <span class="comment">// 如果返回了失败的promise,会用这个失败的原因传给下一个then </span></span><br><span class="line">        <span class="comment">// 如果是成功,那么不会用这个成功的结果，会用调用finally的promise的成功或失败的结果</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve(callback()).then(<span class="function"><span class="params">()</span> =&gt;</span> res)</span><br><span class="line">        &#125;, reason =&gt; &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve(callback()).then(<span class="function"><span class="params">()</span> =&gt;</span> &#123; <span class="keyword">throw</span> reason &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 测试代码 使用promise a+规范测试 </span></span><br><span class="line"><span class="comment">// 安装一个包 promises-aplus-tests </span></span><br><span class="line"><span class="comment">// promises-aplus-test xxx.js</span></span><br><span class="line"><span class="built_in">Promise</span>.defer = <span class="built_in">Promise</span>.deferred = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> dfd = &#123;&#125;;</span><br><span class="line">    dfd.promise = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">        dfd.resolve = resolve;</span><br><span class="line">        dfd.reject = reject;</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">return</span> dfd;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">module</span>.exports = <span class="built_in">Promise</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//  872 passing (19s) 最终测试结果</span></span><br></pre></td></tr></table></figure>

<h2 id="23-模拟new"><a href="#23-模拟new" class="headerlink" title="23.模拟new"></a>23.模拟new</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">copyNew</span>(<span class="params">Ctor, ...args</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 创建一个对象 __proto指向Ctor.prototype</span></span><br><span class="line">    <span class="keyword">const</span> obj = <span class="built_in">Object</span>.create(Ctor.prototype);</span><br><span class="line">    <span class="comment">// 执行Ctor</span></span><br><span class="line">    <span class="keyword">const</span> result = Ctor.call(obj, ...args);</span><br><span class="line">    <span class="comment">// 如果返回的是对象或函数，那么返回这个结果</span></span><br><span class="line">    <span class="keyword">if</span> ((result !== <span class="literal">null</span> &amp;&amp; <span class="keyword">typeof</span> result === <span class="string">'object'</span>) || <span class="keyword">typeof</span> result === <span class="string">'function'</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 如果是原始类型，那么直接忽略，返回创建的对象</span></span><br><span class="line">    <span class="keyword">return</span> obj;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/javascript/2020/10/08/%E4%BD%A0%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84javascript/" rel="prev" title="你不知道的javascript">
      <i class="fa fa-chevron-left"></i> 你不知道的javascript
    </a></div>
      <div class="post-nav-item">
    <a href="/javascript/2020/10/10/compose/" rel="next" title="compose">
      compose <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#常见手写js面试题"><span class="nav-number">1.</span> <span class="nav-text">常见手写js面试题</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-数组扁平化"><span class="nav-number">1.1.</span> <span class="nav-text">1 数组扁平化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-数组去重"><span class="nav-number">1.2.</span> <span class="nav-text">2 数组去重</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-类数组转为真数组"><span class="nav-number">1.3.</span> <span class="nav-text">3 类数组转为真数组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-Array-prototype-filter"><span class="nav-number">1.4.</span> <span class="nav-text">4 Array.prototype.filter()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-Array-prototype-map"><span class="nav-number">1.5.</span> <span class="nav-text">5. Array.prototype.map()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-Array-prototype-forEach"><span class="nav-number">1.6.</span> <span class="nav-text">6. Array.prototype.forEach()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-Array-prototype-reduce"><span class="nav-number">1.7.</span> <span class="nav-text">7. Array.prototype.reduce()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-Function-prototype-apply"><span class="nav-number">1.8.</span> <span class="nav-text">8. Function.prototype.apply()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-Function-prototype-bind"><span class="nav-number">1.9.</span> <span class="nav-text">9. Function.prototype.bind()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-Function-prototype-call"><span class="nav-number">1.10.</span> <span class="nav-text">10. Function.prototype.call()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-Function-prototype-bind"><span class="nav-number">1.11.</span> <span class="nav-text">11. Function.prototype.bind()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-函数防抖-debounce"><span class="nav-number">1.12.</span> <span class="nav-text">12. 函数防抖(debounce)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-函数节流"><span class="nav-number">1.13.</span> <span class="nav-text">13. 函数节流</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#14-柯里化"><span class="nav-number">1.14.</span> <span class="nav-text">14. 柯里化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#15-instanceof"><span class="nav-number">1.15.</span> <span class="nav-text">15. instanceof</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#16-原型继承"><span class="nav-number">1.16.</span> <span class="nav-text">16 原型继承</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#17-Object-is"><span class="nav-number">1.17.</span> <span class="nav-text">17. Object.is()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#18-Object-create"><span class="nav-number">1.18.</span> <span class="nav-text">18. Object.create()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#19-Object-assign"><span class="nav-number">1.19.</span> <span class="nav-text">19. Object.assign()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#20-深拷贝"><span class="nav-number">1.20.</span> <span class="nav-text">20. 深拷贝</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#21-当前网页有多少中html标签"><span class="nav-number">1.21.</span> <span class="nav-text">21. 当前网页有多少中html标签</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#22-Promise"><span class="nav-number">1.22.</span> <span class="nav-text">22. Promise</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#23-模拟new"><span class="nav-number">1.23.</span> <span class="nav-text">23.模拟new</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">HC</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/javascript/archives/">
        
          <span class="site-state-item-count">12</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">HC</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> v4.2.1
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">Theme – <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.7.2
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/javascript/lib/anime.min.js"></script>
  <script src="/javascript/lib/velocity/velocity.min.js"></script>
  <script src="/javascript/lib/velocity/velocity.ui.min.js"></script>

<script src="/javascript/js/utils.js"></script>

<script src="/javascript/js/motion.js"></script>


<script src="/javascript/js/schemes/muse.js"></script>


<script src="/javascript/js/next-boot.js"></script>




  















  

  

</body>
</html>
